package by.apiskun.curex.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.apiskun.curex.dataaccess.UserDAO;
import by.apiskun.curex.datamodel.User;
import by.apiskun.curex.services.UserService;

@Service
public class UserServiceImpl implements UserService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(UserServiceImpl.class);

	@Inject
	private UserDAO dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of UserService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public User get(Long id) {
		User entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(User user) {
		if (user.getId() == null) {
			LOGGER.debug("Save new: {}", user);
			dao.insert(user);
		} else {
			LOGGER.debug("Update: {}", user);
			dao.update(user);
		}
	}

	@Override
	public void delete(User user) {
		LOGGER.debug("Remove: {}", user);
		dao.delete(user.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all users");
		dao.deleteAll();
	}

	@Override
	public Long getCount() {
		Long count = dao.getCount();
		return count;
	}

	@Override
	public List<User> getAllUsers() {
		List<User> users = dao.getAllUsers();
		return users;
	}

	@Override
	public List<User> getAllUsersByFirstName(String firstName) {
		List<User> users = dao.getAllUsersByFirstName(firstName);
		return users;
	}

	@Override
	public List<User> getAllUsersByLastName(String lastName) {
		List<User> users = dao.getAllUsersByLastName(lastName);
		return users;
	}

	@Override
	public List<User> getAllUsersByMidName(String midName) {
		List<User> users = dao.getAllUsersByMidName(midName);
		return users;
	}

	@Override
	public List<User> getAllUsersByFullName(String firstName, String midName,
			String lastName) {
		List<User> users = dao.getAllUsersByFullName(firstName, midName,
				lastName);
		return users;
	}

	@Override
	public void updateUserPassword(Long id, String newPassword) {
		dao.updateUserPassword(id, newPassword);

	}

	@Override
	public void updateUserPhoneNumber(Long id, String newPhoneNumber) {
		dao.updateUserPhoneNumber(id, newPhoneNumber);

	}

	@Override
	public void updateUserLock(Long id, Boolean newUserLock) {
		dao.updateUserLock(id, newUserLock);

	}

	@Override
	public User getUserByLogin(String login) {
		User user = dao.getUserByLogin(login);
		return user;
	}

	@Override
	public List<String> getRoles(Long userId) {
		// TODO Auto-generated method stub
		return null;
	}
}
